﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebApplication1
{
    
    public partial class Select : System.Web.UI.Page
    {
        ConnectionSelect OConSelect = new ConnectionSelect();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillName();
                Fillddl();
            }
            
        }

        public void FillName()
        {
            string Query = "Select * from TBLTEST";
            gvwDetails.DataSource = OConSelect.Select(Query);
            gvwDetails.DataBind();
            
        }

        protected void gvwDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                TextBox txtName = gvwDetails.FooterRow.FindControl("txtNameF") as TextBox;
                TextBox txtAge = gvwDetails.FooterRow.FindControl("txtAgeF") as TextBox;
                TextBox txtAddress = gvwDetails.FooterRow.FindControl("txtAddressF") as TextBox;

                string Query = "INSERT INTO TBLTEST VALUES((Select isnull(max(Id),0) as maxid from TBLTEST) +1 , '" + txtName.Text + "', " + Convert.ToInt32(txtAge.Text) + ", '" + txtAddress.Text + "')";
                OConSelect.Insert(Query);

                FillName();

                txtName.Text = "";
                txtAge.Text = "";
                txtAddress.Text = "";
            }
        }

        protected void gvwDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //Label lblGroupName = gvwDetails.Rows[e.NewEditIndex].FindControl("lblName") as Label;
            //ViewState["OldGroupName"] = lblGroupName.Text;

            //gvwDetails.EditIndex = e.NewEditIndex;
            //FillName();



            Label lblName = gvwDetails.Rows[e.NewEditIndex].FindControl("lblName") as Label;
            Label lblAge = gvwDetails.Rows[e.NewEditIndex].FindControl("lblAge") as Label;
            Label lblAddress = gvwDetails.Rows[e.NewEditIndex].FindControl("lblAddress") as Label;

            gvwDetails.EditIndex = e.NewEditIndex;
            FillName();


            TextBox txtName = gvwDetails.Rows[e.NewEditIndex].FindControl("txtName") as TextBox;
            TextBox txtAge = gvwDetails.Rows[e.NewEditIndex].FindControl("txtAge") as TextBox;
            TextBox txtAddress = gvwDetails.Rows[e.NewEditIndex].FindControl("txtAddress") as TextBox;


        }

        protected void gvwDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtName = gvwDetails.Rows[e.RowIndex].FindControl("txtName") as TextBox;
            TextBox txtAge = gvwDetails.Rows[e.RowIndex].FindControl("txtAge") as TextBox;
            TextBox txtAddress = gvwDetails.Rows[e.RowIndex].FindControl("txtAddress") as TextBox;

            ImageButton ibtnUpdate = gvwDetails.Rows[e.RowIndex].FindControl("ibtnUpdate") as ImageButton;


            string Query = "Update TBLTEST SET  Name = '" + txtName.Text + "', Age = " + txtAge.Text + ", Address = '" + txtAddress.Text + "' WHERE Id = "+ Convert.ToInt32(ibtnUpdate.CommandArgument) +" ";
            OConSelect.Insert(Query);            
            gvwDetails.EditIndex = -1;
            FillName();
        }

        protected void gvwDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwDetails.EditIndex = -1;
            FillName();
        }

        protected void gvwDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            ImageButton ibtnDelete = gvwDetails.Rows[e.RowIndex].FindControl("ibtnDelete") as ImageButton;

            string Query = " DELETE FROM TBLTEST WHERE Id = " + Convert.ToInt32(ibtnDelete.CommandArgument) + " ";
            OConSelect.Insert(Query);
            FillName();
        }

        protected void gvwDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvwDetails.PageIndex = e.NewPageIndex;
            FillName();
        }



        private void Fillddl()
        {
            String Query = " Select Id, Name from TBLTEST";
            ddlName.DataSource = OConSelect.Select(Query);
            ddlName.DataValueField = "Id";
            ddlName.DataTextField = "Name";
            ddlName.DataBind();
        }


    }
}